/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * BaseOperateRecordController
 * 操作记录
 * 修改纪录
 * 2020-10-30 版本：1.0 zhanglongfei 创建。
 *
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2020-10-30
 */
package com.topscomm.base.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.topscomm.basic.exception.BusinessException;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.BaseSystemConst.BaseOperateTypeEnum;
import com.topscomm.base.controller.auto.BaseOperateRecordControllerAuto;
import com.topscomm.base.pojo.BaseOperateRecordEntity;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: zhanglongfei
 * @date: 2020-10-30
 * @description:操作记录Controller
 * @modify:
 */
@RestController
@RequestMapping("base/operateRecord")
public class BaseOperateRecordController extends BaseOperateRecordControllerAuto
		implements IBaseOperateRecordController {
	/**
	 * @author: zhanglongfei
	 * @date: 2020-10-30
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourcetype")) {
			String value = paraMap.get("qry_sourcetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourcetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourceid")) {
			String value = paraMap.get("qry_sourceid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourceid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_operatetype")) {
			String value = paraMap.get("qry_operatetype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and operatetype = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_operateresult")) {
			String value = paraMap.get("qry_operateresult").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and operateresult = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_usercode")) {
			String value = paraMap.get("qry_usercode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and usercode = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: zhanglongfei
	 * @date: 2020-10-30
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description:类似关注、收藏等操作，第一次调用是增加，第二次取消，第三次增加...
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020年10月30日下午4:17:31
	 * @modify:
	 */
	@PostMapping(value = "operateState")
	public ResponseResult<Object> operateState(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendLineSingle("sourcetype='{0}' and sourceid='{1}'", paraMap.get("sourcetype"), paraMap.get("sourceid"));
			sql.appendLineSingle("and operatetype='{0}' and usercode='{1}'", paraMap.get("operatetype"), this.getUserBean().getUser().getCode());
			int count = this.service.getCount(sql.toString());
			if (count > 0) {
				this.service.deleteByWhere(sql.toString());
			} else {
				paraMap.put(BaseOperateRecordEntity.FieldUsercode, this.getUserBean().getUser().getCode());
				paraMap.put(BaseOperateRecordEntity.FieldUsername, this.getUserBean().getUser().getName());
				paraMap.put(BaseOperateRecordEntity.FieldOperatedate, new Date());
				insert(paraMap);
			}
			return ResponseResult.ok(count > 0 ? "off" : "on");
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @description:类似阅读的操作，若没有记录，则新增，若有，则更新
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020年12月12日上午9:16:09
	 * @modify:
	 */
	@PostMapping(value = "operateUpdate")
	public ResponseResult<Object> operateUpdate(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			if (StringUtil.isEmpty(ConvertUtil.convertToString(paraMap.get("sourceid")))) {
				return ResponseResult.error("参数错误！");
			}
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendLineSingle("sourcetype='{0}' and sourceid='{1}'", paraMap.get("sourcetype"), paraMap.get("sourceid"));
			sql.appendLineSingle("and operatetype='{0}' and usercode='{1}'", paraMap.get("operatetype"), this.getUserBean().getUser().getCode());
			int count = this.service.getCount(sql.toString());
			paraMap.put(BaseOperateRecordEntity.FieldOperatedate, new Date());
			if (count > 0) {
				this.service.updateByWhere(paraMap, sql.toString());
			} else {
				paraMap.put(BaseOperateRecordEntity.FieldUsercode, this.getUserBean().getUser().getCode());
				paraMap.put(BaseOperateRecordEntity.FieldUsername, this.getUserBean().getUser().getName());
				insert(paraMap);
			}
			return ResponseResult.ok("操作成功");
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/***
	 * @description:查询列表
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020年11月9日上午11:37:12
	 * @modify:
	 */
	@GetMapping(value = "queryList")
	public ResponseResult<Object> queryList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			if (paraMap.containsKey("order")) {
				sqlWhere += " order by " + paraMap.get("order");
			}
			List<BaseOperateRecordEntity> list = this.service.queryByWhere(sqlWhere);
			return ResponseResult.ok(list);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @description: 查询是否关注
	 * @param paraMap
	 * @return
	 * @author: maxiaodong
	 * @date: 2021年1月27日下午3:54:30
	 * @modify:
	 */
	@GetMapping(value = "/queryMark")
	public ResponseResult<Object> queryMark(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendLineSingle("sourcetype='{0}' and sourceid='{1}'", paraMap.get("sourcetype"), paraMap.get("sourceid"));
			sql.appendLineSingle("and operatetype='{0}' and usercode='{1}'", BaseOperateTypeEnum.Mark, this.getUserBean().getUser().getCode());
			int count = this.service.getCount(sql.toString());
			Map<String, Object> result = new HashMap<>();
			if (count > 0) {
				result.put("markflag", true);
			} else {
				result.put("markflag", false);
			}
			return ResponseResult.ok(result);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}
	/**
	 *
	 * @description:
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2020-12-01
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		super.dataCodeList(paraMap);
		Map<String, Object> codeMap = new HashMap<String, Object>();

		List<Map<String, Object>> operatetypeList = datacodeCache.getCodeListMap("FIleOperateType", "", "sortcode");
		codeMap.put("operatetypeList", operatetypeList);
		List<Map<String, Object>> enabledList = datacodeCache.getCodeListMap("CboYesNo", "", "sortcode");
		codeMap.put("enabledList", enabledList);
		List<Map<String, Object>> markSourceTypeList = datacodeCache.getCodeListMap("MarkSourceType", "", "sortcode");
		codeMap.put("markSourceTypeList", markSourceTypeList);
		return ResponseResult.ok(codeMap);
	}
	/**
	 * @description: 查询Itr中关注的列表
	 * @param paraMap
	 * @return
	 * @author: zhangqianbo
	 * @date:
	 * @modify:
	 */
	@GetMapping(value = "/queryItrMark")
	public ResponseResult<Object> queryItrMark(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String code = this.getUserBean().getUser().getCode();
			paraMap.put("userCode", code);
			int totalCount = this.service.getCount1(paraMap, "queryItrMark");
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				paraMap.put("IsNeedPage", true);
				// 分页拦截处理
				int beginCount = query.getCurrentPage() * query.getPageSize() - query.getPageSize();
				paraMap.put("begincount", beginCount);
				paraMap.put("pagesize", query.getPageSize());
				tempList = this.service.queryMapForPage(query.getCurrentPage(), query.getPageSize(), query.getSidx(),
						query.getSord(), paraMap, "queryItrMark");
			} else {
				tempList = this.service.queryMap(paraMap, "queryItrMark");
			}
			this.service.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}


	/**
	 * @description: 导出我的关注数据
	 * @author: zhangqianbo
	 * @date:
	 * @modify:
	 */
	@PostMapping(value = "/exportItrMarkExcel")
	public void exportItrMarkExcel(@RequestBody(required = false) Map<String, Object> paraMap, HttpServletRequest request,
									  HttpServletResponse response) {
		try {
			String code = this.getUserBean().getUser().getCode();
			paraMap.put("userCode", code);
			List<Map<String, Object>> listDataMap = this.service.queryMap(paraMap, "queryItrMark");
			this.service.setDispFields(listDataMap);
			this.exportBaseExcel(paraMap, request, response, listDataMap, "我的关注.xlsx");
		} catch (Exception ex) {
			throw new BusinessException(ex.getMessage(), true);
		}
	}

}
